package com.example.chartbar.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by liangkun on 2018/8/9.
 * 作用:
 */

public class DateUtil {

    /**
     * 指定日期格式 yyyy-MM-dd
     */
    public static final String DATE_FORMAT_YMD = "yyyy-MM-dd";
    public static String getDate(int i) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DAY_OF_MONTH, i);
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YMD);
        String dateStr = sdf.format(c.getTime());

        return dateStr;
    }


    /**
     * @param strDate 如218-05-06
     * @return year 如:5
     */
    public static String getMonthFromDate(String strDate) {
        Date date = getDate(strDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        return String.valueOf(month);
    }

    /**
     * @param strDate 2018-05-10
     * @return Date格式
     */
    public static Date getDate(String strDate) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YMD);
        Date date = null;
        try {
            date = format.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * @param strDate 如218-05-06
     * @return year 如:6
     */
    public static String getDayFromDate(String strDate) {
        Date date = getDate(strDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return String.valueOf(day);
    }
}
